/* 
 * Class to manage the pir chart
 */

var Soroban = new Object ();

Soroban.PieChart = function ( options ) {

    var from = new Date ();
    var to = new Date ();

    var opt = options;

    this.init = function () {
        // Get dates
        //this.from = new Date();
        from.setDate (1);

//        this.to = new Date ();
        to.setMonth ( to.getMonth() + 1 );
        to.setDate (1);
        
    };

    this.drawChart = function () {

        jQuery.ajax({
            url: opt.url_base + '/getChartData?from_date='
                + this.getFromDate()
                + '&to_date=' 
                + this.getToDate(),
            dataType: 'json',
            success: function(json_data, textStatus, jqXHR){

                // Create our data table.
                var data = new google.visualization.DataTable();
                data.addColumn('string', 'Label');
                data.addColumn('number', 'Amount');
                data.addRows(json_data);

                // Instantiate and draw our chart, passing in some options.
                var chart = new google.visualization.PieChart(document.getElementById('chart_div'));
                chart.draw(data, {width: 400, height: 240, is3D: true, 
                        title: "Transactions between: " + from.toString() + ' and ' + to.toString()
                    });
            }
        });
    };

    this.getFromDate = function () {
        return dateToString (from);
    };

    this.getToDate = function () {
        return dateToString (to);
    };

    this.moveDate = function (months_to_move) {
        dateDisplacement (from, months_to_move);
        dateDisplacement (to, months_to_move);
    }

    var dateToString = function (date) {
        return date.getFullYear().toString () + '-'
            + ( date.getMonth() + 1 ).toString() + '-'
            + date.getDate();
    };

    var dateDisplacement = function (date, months_to_move) {
        current = date.getMonth();
        date.setMonth (current + months_to_move);
    }
};


